<?php

namespace app\company\controller\report\subset;

use app\BaseController;
use app\model\DepartmentAdminModel;
use app\model\ProxyEvaluationResultModel;
use app\model\RatingScaleModel;
use app\model\UnitModel;
use app\model\UserModel;
use think\App;

class ProxyReport extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }


    /**
     * 他人代测报告列表
     * @return bool|string
     */
    public function Index(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'unit_id|单位ID' => 'require'
            ]);
            $where = [];
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where[] = ['time', 'between', [$param['start_time'], $param['end_time']]];
            } else {
                if (!empty($param['start_time'])) {
                    $where[] = ['time', '>=', $param['start_time']];
                }
                if (!empty($param['end_time'])) {
                    $where[] = ['time', '<=', $param['end_time']];
                }
            }
            if (!empty($param['name'])) {
                $RatingScaleModel = new RatingScaleModel();
                $UserModel = new UserModel();
                $rating_scale_ids = $RatingScaleModel->where('name', 'like', '%' . $param['name'] . '%')->column('id');
                $uids = $UserModel->where('name', 'like', '%' . $param['name'] . '%')->column('id');
                if (count($rating_scale_ids) > 0 || count($uids) > 0) {
                    if (count($rating_scale_ids) > 0) {
                        $where[] = ['rating_scale_id', 'in', $rating_scale_ids];
                    }
                    if (count($uids) > 0) {
                        $where[] = ['uid', 'in', $uids];
                    }
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            if (!empty($param['type_id'])) {
                $RatingScaleModel = new RatingScaleModel();
                $rating_scale_ids = $RatingScaleModel->where('type_id', '=', $param['type_id'])->column('id');
                if (count($rating_scale_ids) > 0) {
                    $where[] = ['rating_scale_id', 'in', $rating_scale_ids];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            if (!empty($param['answer_role'])) {
                $where[] = ['answer_role', '=', $param['answer_role']];
            }
            if (!empty($param['is_explain'])) {
                $where[] = ['is_explain', '=', $param['is_explain']];
            }
            if (isset($param['is_warning']) && strlen($param['is_warning']) > 0) {
                $where[] = ['is_warning', '=', $param['is_warning']];
            }
            if (isset($param['proxy_uid']) && strlen($param['proxy_uid']) > 0) {
                $where[] = ['proxy_uid', '=', $param['proxy_uid']];
            }
            if (!empty($param['id'])) {
                if (is_array($param['id'])) {
                    $where[] = ['id', 'in', $param['id']];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            $where[] = ['unit_id', '=', $param['unit_id']];
            $ProxyEvaluationResultModel = new ProxyEvaluationResultModel();
            $query = $ProxyEvaluationResultModel->where($where);
            $data = $query->field('id,proxy_uid,uid,rating_scale_id,answer_role,total_score,time,result_read_auth,is_warning,warning_id,is_explain,is_special_group')
                ->with([
                    'user' => function ($query) {
                        $query->with(['departmentName', 'unitName']);
                    },
                    'ratingScale' => function ($query) {
                        $query->with(['ratingScaleTypeName']);
                    },
                    'ProxyUser',
                    'RatingScaleFactorWarningModel'
                ])
                ->order('id desc')->paginate($param['pageSize'])->toArray();
            $departmentIds = array_column($data['data'], 'department_id');
            $DepartmentAdminModel = new DepartmentAdminModel();
            $departmentList = $DepartmentAdminModel->where('department_id', 'in', $departmentIds)->with(['userName'])->select()->toArray();
            $departmentData = [];
            foreach ($departmentList as $item) {
                $departmentData[$item['department_id']][] = $item['user_name'];
            }
            foreach ($data['data'] as &$item) {
                $item['department_admins'] = $departmentData[$item['department_id']] ?? [];
            }
            $data['ids'] = $query->column('id');
            $UnitModel = new UnitModel();
            $data['unit_name'] = $UnitModel->where('id','=',$param['unit_id'])->cache(true,60)->value('name');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }
}